package com.xinjiang.electricpower.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO;
import com.xinjiang.electricpower.domain.vo.EventProcessingVO;
import com.xinjiang.electricpower.domain.vo.MyEarlyWarningVO;
import com.xinjiang.electricpower.domain.vo.WorkOrderPoolSummaryVO;
import com.xinjiang.electricpower.service.IWorkOrderWarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author feizhao
 * @date 2022/11/17
 * @apiNote 工单预警控制层类
 */
@RestController
@RequestMapping("/xinjiang-power-supply-station/work-order-warning/")
@Api(value = "工单中心-工单预警", tags = "工单中心-工单预警")
@Validated//有此注解，才会校验游离的参数
@Slf4j
@RequiredArgsConstructor
public class WorkOrderWarningController extends BaseController {

    private final IWorkOrderWarningService workOrderEarlyWarningService;

    /**
     * @return AjaxResult 响应结果
     * @description 工单预警各个预警类型完成情况统计
     */
    @GetMapping("/count")
    @ApiOperation(value = "工单预警各个预警类型完成情况统计", notes = "工单预警各个预警类型完成情况统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderEarlyWarningDTO", value = "工单预警DTO类", paramType = "query", dataType = "WorkOrderEarlyWarningDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO.class),
    })
    public AjaxResult getOrderNum(WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO) {
        List<EventProcessingVO> eventProcessing = workOrderEarlyWarningService.selectGdWarningTypesCount(workOrderEarlyWarningDTO);
        return success(eventProcessing);
    }

    /**
     * @param workOrderEarlyWarningDTO 工单预警实体类
     * @param page                     分页对象
     * @return R<IPage < MyEarlyWarningVO>> 工单预警列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询待处理、已完成、处理中的工单", notes = "查询待处理、已完成、处理中的工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderEarlyWarningDTO", value = "工单预警DTO类", paramType = "query", dataType = "WorkOrderEarlyWarningDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO.class),
            @ApiImplicitParam(name = "page", value = "分页对象", paramType = "query", dataType = "PageDTO<XjOrderGeneralVO>", dataTypeClass = com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO.class)
    })
    public R<PageInfo<MyEarlyWarningVO>> getXjOrderGeneralProcess(WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO, PageDTO<MyEarlyWarningVO> page) {
        PageInfo<MyEarlyWarningVO> pages = workOrderEarlyWarningService.selectMyEarlyWarningPage(page, workOrderEarlyWarningDTO);
        return R.ok(pages, "查询成功");
    }

    /**
     * @return R<Long> 工单总数
     * @description 工单预警按照各个类型汇总工单
     */
    @GetMapping("/count-status")
    @ApiOperation(value = "工单预警按照各个类型汇总工单", notes = "工单预警按照各个类型汇总工单")
    public R<Map<String ,Object>> getWorkOrderPoolSummaryNum(WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO) {
        List<WorkOrderPoolSummaryVO> items = workOrderEarlyWarningService.getXjOrderGeneralByStatus(workOrderEarlyWarningDTO);
        AtomicReference<Long> sum = new AtomicReference<>(0L);
        items.forEach(item ->{
            sum.updateAndGet(v -> v + item.getNum());
        });
        Map<String ,Object> map = new HashMap<>();
        map.put("sum",sum.get());
        map.put("items",items);
        return R.ok(map, "查询成功");
    }
    /**
     * 预警工单详情
     */
    @ApiOperation("预警工单详情")
    @Log(title = "预警工单详情")
    @GetMapping("/detail/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工单id", dataType = "string", paramType = "path", required = true, dataTypeClass = java.lang.String.class),
    })
    public R<Object> getMyWorkWarning(@NotNull(message = "id不能为null") @PathVariable String id) {
        MyEarlyWarningVO myEarlyWarningVO = workOrderEarlyWarningService.selectMyEarlyWarningDetail(id);
        return R.ok(myEarlyWarningVO, "查询成功");
    }

    /**
     * 导出预警工单
     */
    @ApiOperation("导出预警工单")
    @Log(title = "导出预警工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderEarlyWarningDTO", value = "workOrderEarlyWarningDTO类的ids属性", required = true, paramType = "body", dataType = "WorkOrderEarlyWarningDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO.class),
            @ApiImplicitParam(name = "response", value = "响应流", dataType = "HttpServletResponse", dataTypeClass = javax.servlet.http.HttpServletResponse.class)
    })
    public void export(HttpServletResponse response, @RequestBody WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO) {
        List<MyEarlyWarningVO> list = workOrderEarlyWarningService.selectMyEarlyWarnings(workOrderEarlyWarningDTO.getIds());
        ExcelUtil<MyEarlyWarningVO> util = new ExcelUtil<>(MyEarlyWarningVO.class);
        util.exportExcel(response, list, "待处理、已完成、处理中的工单数据");
    }

    /**
     *
     */
    @ApiOperation("导出预警工单")
    @Log(title = "导出预警工单", businessType = BusinessType.EXPORT)
    @PostMapping("/exports")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "workOrderEarlyWarningDTO", value = "预警工单DTO实体类", paramType = "body", dataType = "WorkOrderEarlyWarningDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO.class),
            @ApiImplicitParam(name = "response", value = "响应流", dataType = "HttpServletResponse", dataTypeClass = javax.servlet.http.HttpServletResponse.class)
    })
    public void exports(HttpServletResponse response, @RequestBody WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO) {

        List<MyEarlyWarningVO> list = workOrderEarlyWarningService.selectMyEarlyWarnings(workOrderEarlyWarningDTO);
        ExcelUtil<MyEarlyWarningVO> util = new ExcelUtil<>(MyEarlyWarningVO.class);
        util.exportExcel(response, list, "导出预警工单数据");
    }
    /**
     * @return R<Long> 返回结果
     * @description 标记处理
     */
    @PutMapping("/processing-mark")
    @Log(title = "标记处理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "标记处理", notes = "标记处理")
    public R<Object> statement(@RequestBody WorkOrderEarlyWarningDTO workOrderEarlyWarningDTO) {
        int flag = workOrderEarlyWarningService.updateProcessingMark(workOrderEarlyWarningDTO);
        if(flag>0){
            return R.ok();
        }
        return R.fail("操作失败");
    }
}
